<template>
  <a-card :bordered="false">
    <div class="card-title">{{ $route.meta.title }}</div>
    <div class="table-operator">
      <a-button v-action:add type="primary" icon="plus" @click="handleAdd">{{$t('app.add')}}</a-button>
    </div>
    <s-table
      ref="table"
      rowKey="help_id"
      :loading="isLoading"
      :columns="columns"
      :data="loadData"
      :pageSize="15"
    >
      <!-- 内容 -->
      <span slot="content" slot-scope="text">
        <p class="twoline-hide" style="width: 420px;">{{ text }}</p>
      </span>
      <!-- 操作 -->
      <span slot="action" slot-scope="text, item">
        <a v-action:edit style="margin-right: 8px;" @click="handleEdit(item)">{{$t('app.edit')}}</a>
        <a v-action:delete @click="handleDelete(item)">{{$t('app.del')}}</a>
      </span>
    </s-table>
    <AddForm ref="AddForm" @handleSubmit="handleRefresh" />
    <EditForm ref="EditForm" @handleSubmit="handleRefresh" />
  </a-card>
</template>

<script>
import * as Api from '@/api/content/help'
import { STable } from '@/components'
import { AddForm, EditForm } from './modules'

export default {
  name: 'Index',
  components: {
    STable,
    AddForm,
    EditForm
  },
  data () {
    return {
      // 查询参数
      queryParam: {},
      // 正在加载
      isLoading: false,
      // 表头
      columns: [
        {
          title: this.$t('app.help')+'ID',
          dataIndex: 'help_id'
        },
        {
          title: this.$t('app.title'),
          dataIndex: 'title'
        },
        {
          title: this.$t('app.content'),
          dataIndex: 'content',
          scopedSlots: { customRender: 'content' }
        },
        {
          title: this.$t('app.sort'),
          dataIndex: 'sort'
        },
        {
          title: this.$t('app.update')+this.$t('app.time'),
          dataIndex: 'update_time'
        },
        {
          title: this.$t('app.action'),
          dataIndex: 'action',
          width: '180px',
          scopedSlots: { customRender: 'action' }
        }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: param => {
        return Api.list({ ...param, ...this.queryParam })
          .then(response => {
            return response.data.list
          })
      }
    }
  },
  created () {

  },
  methods: {

    /**
     * 新增记录
     */
    handleAdd () {
      this.$refs.AddForm.add()
    },

    /**
     * 编辑记录
     */
    handleEdit (item) {
      this.$refs.EditForm.edit(item)
    },

    /**
     * 删除记录
     */
    handleDelete (item) {
      const app = this
      const modal = this.$confirm({
        title: app.$t('content.article.tip1')+'?',
        content: app.$t('content.article.tip2'),
        onOk () {
          return Api.deleted({ helpId: item.help_id })
            .then((result) => {
              app.$message.success(result.message, 1.5)
              app.handleRefresh()
            })
            .finally(result => {
              modal.destroy()
            })
        }
      })
    },

    /**
     * 刷新列表
     * @param Boolean bool 强制刷新到第一页
     */
    handleRefresh (bool = false) {
      this.$refs.table.refresh(bool)
    },

    /**
     * 检索查询
     */
    onSearch () {
      this.handleRefresh(true)
    }

  }
}
</script>
